package com.shenzhen.horselive.widget;

import android.util.SparseArray;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;

import com.flqy.datepicker.NumberPicker;
import com.shenzhen.horselive.App;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.base.BaseBottomPushDialogFragment;
import com.shenzhen.horselive.common.manager.PlaceManager;


import java.util.ArrayList;


/**
 * Created by liurenhui on 15/3/25.
 */
public class AreaPicker extends BaseBottomPushDialogFragment implements View.OnClickListener {
    private NumberPicker provinceView;
    private NumberPicker cityView;
    private TextView cancelView;
    private TextView titleView;
    private TextView finishView;
    private ArrayList<String> provinceList;
    private SparseArray<ArrayList<String>> cityMap;

    private AreaPickerListener listener;
    private boolean unlimited = false;

    @Override
    protected int getContentViewId() {
        return R.layout.area_picker;//	contentView = mContext.getLayoutInflater().inflate(R.layout.area_picker, null);
    }

    @Override
    public void onInflateView(View contentView) {
        super.onInflateView(contentView);
        provinceView = (NumberPicker) contentView.findViewById(R.id.wheel_province);
        cityView = (NumberPicker) contentView.findViewById(R.id.wheel_city);
        cancelView = (TextView) contentView.findViewById(R.id.cancel);
        titleView = (TextView) contentView.findViewById(R.id.title);
        finishView = (TextView) contentView.findViewById(R.id.finish);

    }

    public void setUnlimited(boolean unlimited) {
        this.unlimited = unlimited;
    }

    @Override
    public void onPrepareData() {
        super.onPrepareData();
        PlaceManager.Region region = unlimited ? App.getInstance().getPlaceManager().getRegionUnlimited() : App.getInstance().getPlaceManager().getRegion();
        provinceList = region.provinces;
        cityMap = region.cities;
        provinceView.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                updateCity();
            }
        });
        updateProvince(0);
        String[] displayValues = new String[provinceList.size()];
        provinceView.setMinValue(0);
        provinceView.setMaxValue(displayValues.length - 1);
        provinceView.setDisplayedValues(provinceList.toArray(displayValues));
        provinceView.setSelectorWheelItemCount(5);
        provinceView.setWrapSelectorWheel(false);
        finishView.setOnClickListener(this);
        cancelView.setOnClickListener(this);


    }

    public interface AreaPickerListener {
        void onPicked(String province, String city);
    }


    @Override
    public void onClick(View v) {
        if (v == cancelView) {
            dismiss();
        } else if (v == finishView) {
            if (listener != null) {
                String province = provinceList.get(provinceView.getValue());
                String city = cityMap.get(provinceView.getValue()).get(cityView.getValue());
                listener.onPicked(province, city);
            }
            dismiss();
        }
    }

    public void setTitle(String title) {
        if (title == null)
            title = "";
        titleView.setText(title);
    }


    private void updateProvince(int index) {
        provinceView.setValue(index);
        updateCity();
    }

    private void updateCity() {
        int provinceIndex = provinceView.getValue();
        ArrayList<String> cityList = cityMap.get(provinceIndex);
        String[] displayValues = new String[cityList.size()];

        cityView.setSelectorWheelItemCount(5);


        cityView.setDisplayedValues(null);
        cityView.setMinValue(0);
        cityView.setMaxValue(displayValues.length - 1);
        cityView.setDisplayedValues(cityList.toArray(displayValues));
//		cityView.setWrapSelectorWheel(false);
    }


    public void setListener(AreaPickerListener listener) {
        this.listener = listener;
    }

    public static AreaPicker show(FragmentManager manager, AreaPickerListener listener) {
        AreaPicker picker = new AreaPicker();
        picker.show(manager);
        picker.setListener(listener);
        return picker;
    }


}
